package com.zjt.spring.boot.learn3.service.impl;

import com.zjt.spring.boot.learn3.mapper.UserMapper;
import com.zjt.spring.boot.learn3.model.dto.UserDataDto;
import com.zjt.spring.boot.learn3.model.entity.User;
import com.zjt.spring.boot.learn3.model.vo.UserDataVO;
import com.zjt.spring.boot.learn3.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 获取用户列表
     */
    @Override
    public List<UserDataVO> getUserList(UserDataDto requestDto) {
        return userMapper.getUserList(requestDto);
    }

    /**
     * 通过id获取用户详情
     */
    @Override
    public UserDataVO getUserDetailById(UserDataDto requestDto) {
        return (UserDataVO) userMapper.getUserDetailById(requestDto);
    }

    /**
     * 新增单条用户
     */
    @Override
    public Boolean insertUser(UserDataDto requestDto) {
        User user = new User();
        BeanUtils.copyProperties(requestDto, user);
        return userMapper.insertUser(user) > 0;
    }

    /**
     * 新增多条用户
     */
    @Override
    public Boolean insertUsers(List<UserDataDto> dtoList) {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < dtoList.size(); i++) {
            User user = new User();
            BeanUtils.copyProperties(dtoList.get(i), user);
            userList.add(user);
        }
        return userMapper.insertUsers(userList) > 0;
    }

    /**
     * 单条用户更新
     */
    @Override
    public Boolean updateUser(UserDataDto requestDto) {
        return userMapper.updateUser(requestDto);
    }

    /**
     * 通过id删除用户
     */
    @Override
    public Boolean deleteUserById(Integer id) {
        return userMapper.deleteUserById(id);
    }
}